let Util = {

}

// ff21b90b-6eed-4747-bd97-6224fbc2a550
Util.CHAR_KEYCODE_MAPPING = {
    "0" : "KEYCODE_0",
    "1" : "KEYCODE_1",
    "2" : "KEYCODE_2",
    "3" : "KEYCODE_3",
    "4" : "KEYCODE_4",
    "5" : "KEYCODE_5",
    "6" : "KEYCODE_6",
    "7" : "KEYCODE_7",
    "8" : "KEYCODE_8",
    "9" : "KEYCODE_9",
    "a" : "KEYCODE_A",
    "b" : "KEYCODE_B",
    "c" : "KEYCODE_C",
    "d" : "KEYCODE_D",
    "e" : "KEYCODE_E",
    "f" : "KEYCODE_F",
    "g" : "KEYCODE_G",
    "h" : "KEYCODE_H",
    "i" : "KEYCODE_I",
    "j" : "KEYCODE_J",
    "k" : "KEYCODE_K",
    "l" : "KEYCODE_L",
    "m" : "KEYCODE_M",
    "n" : "KEYCODE_N",
    "o" : "KEYCODE_O",
    "p" : "KEYCODE_P",
    "q" : "KEYCODE_Q",
    "r" : "KEYCODE_R",
    "s" : "KEYCODE_S",
    "t" : "KEYCODE_T",
    "u" : "KEYCODE_U",
    "v" : "KEYCODE_V",
    "w" : "KEYCODE_W",
    "x" : "KEYCODE_X",
    "y" : "KEYCODE_Y",
    "z" : "KEYCODE_Z",
};

Util.download = function(url,path){
    let res = http.get(url);
    let bytes = null;
    if(res.statusCode != 200){
        toastLog('download failed url = ' , url);
    }else{
        bytes = res.body.bytes;
        if(path){
            files.writeBytes(path, res.body.bytes())
        }
    }
    return bytes;
}

Util.readImage = function(path){
    let bytes = images.read(path);
    return bytes;
}

Util.textClick = function(str){
    
    let manualSettings = text(str).findOne(5000); // 等待最多 5 秒
    // sleep(1000)
    if (manualSettings != null) {
        let bounds = manualSettings.bounds();
        if (bounds) {
            console.log(`text:${str}找到控件找到，模拟点击`);
            click(bounds.centerX(), bounds.centerY()); // 模拟点击控件中心
        } else {
            console.log(`未获取到控件text:${str}的边界信息`);
        }
    } else {
        console.log("未找到目标控件");
    }
}

Util.idClick = function(resId){
    
    let manualSettings = id(resId).findOne(5000); // 等待最多 5 秒
    // sleep(1000)
    if (manualSettings != null) {
        let bounds = manualSettings.bounds();
        if (bounds) {
            console.log(`id:${resId}找到控件找到，模拟点击`);
            click(bounds.centerX(), bounds.centerY()); // 模拟点击控件中心
        } else {
            console.log(`未获取到控件id:${resId}的边界信息`);
        }
    } else {
        console.log("未找到目标控件");
    }
}

Util.getKeyCodesWithIP = function(ip){
    let chars = ip.split("");
    let keycodes = [];
    for(let i = 0; i < chars.length; i++){
        let ch = chars[i];
        if(ch == '.'){
            keycodes.push("KEYCODE_PERIOD");
        }else{
            keycodes.push(`KEYCODE_${ch}`);
        }
    }
    return keycodes;
}

Util.getKeyCodesWithString = function(str){
    let chars = str.split("");
    let keycodes = [];
    for(let i = 0; i < chars.length; i++){
        let ch = chars[i];
        let keycode = '';
        if(ch == '-'){
            keycode = "KEYCODE_MINUS";
        }else{
            keycode = Util.CHAR_KEYCODE_MAPPING[ch];
        }
        keycodes.push(keycode);
    }
    console.log('keycodes = ' , keycodes);
    return keycodes;
}

Util.clearAppData = function(packageName){
    if (!packageName) {
        console.log("请指定要清除的应用包名！");
        return;
    }
    // 执行 pm clear 命令，要求设备已 root
    var result = shell("pm clear " + packageName, true);
    if (result.code === 0) {
        console.log("成功清除 " + packageName + " 的缓存和数据！");
    } else {
        console.log("清除失败，可能没有 root 权限或包名无效。");
    }
}

Util.killApp = function(packageName){
    if (!packageName) {
        console.log("请指定要结束进程的应用包名！");
        return;
    }

    // 使用 shell 命令强制停止应用
    var result = shell("am force-stop " + packageName, true);

    if (result.code === 0) {
        console.log("成功结束 " + packageName + " 的进程！");
    } else {
        console.log("结束进程失败，可能没有 root 权限或包名无效。");
    }
}

Util.setCurrentEditText = function(str){
    let focusedWidget = classNameMatches(/.*/).focused().findOne();
    if(!focusedWidget){
        return false;
    }
    let className = focusedWidget.className();
    if(className != "android.widget.EditText"){
        return false;
    }
    focusedWidget.setText(str);
}

module.exports = Util;